<?php
/**祝福语
 * Created by PhpStorm.
 * User: xf
 * Date: 2018/6/13
 * Time: 16:02
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Http\Utils\Time;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class BlessingController extends Controller
{
    public function fileAddBlessivngTextType()
    {
        return view('admin.index.blessing.filedddblessingtype');
    }

    /**
     * 添加祝福语类型
     * @param Request $request
     * @return \App\Http\Utils\Response
     */
    public function addBlessingTextType(Request $request)
    {
        $data = $request->all();
        if (empty($name)) {
            return $this->json('', 417, '类型名不能为空');
        }
        DB::table('blessing_text_type')
            ->insert([
                'path'        => $data['path'],
                'name'        => $data['name'],
                'update_time' => Time::current(),
                'create_time' => Time::current(),
            ]);
        return $this->json('', 200, 'ok');
    }

    /**
     * 获取所有
     * @return \App\Http\Utils\Response
     */
    public function getBlessingTextTypeList()
    {
        $dt = DB::table('blessing_text_type')
            ->get()->toArray();
        return $this->json($dt, 200, 'ok');
    }

    /**
     * 获取一条
     * @return \App\Http\Utils\Response
     */
    public function getOneBlessingTextType($id)
    {
        $dt = DB::table('blessing_text_type')
            ->where('id', '=', $id)
            ->first();
        return $this->json($dt, 200, 'ok');
    }

    /**
     * 修改祝福语类型
     * @param Request $request
     * @return \App\Http\Utils\Response
     */
    public function updateBlessingTextType(Request $request, $id)
    {
        $data = $request->all();
        if (empty($name)) {
            return $this->json('', 417, '类型名不能为空');
        }
        DB::table('blessing_text_type')
            ->where('id', '=', $id)
            ->update([
                'path'        => $data['path'],
                'name'        => $data['name'],
                'update_time' => Time::current(),
            ]);
        return $this->json('', 200, 'ok');
    }


    public function fileAddBlessingText()
    {
        $dt = DB::table('blessing_text_type')
            ->get()->toArray();
        return view('admin.index.blessing.fileaddblessing', [
            'type' => $dt
        ]);
    }


    public function addBlessingText(Request $request)
    {
        $data = $request->all();
        $rules = [
            'blessing_text_type_id' => 'required',
            'content'               => 'required',
        ];
        $messsages = [
            'blessing_text_type_id.required' => '类型不能为空',
            'content.required'               => '内容不能为空'
        ];
        $validator = Validator::make($data, $rules, $messsages);
        if ($validator->fails()) {
            return $this->json('', 417, $validator->getMessageBag()->getMessages());
        }
        DB::table('blessing_text')
            ->insert([
                'blessing_text_type_id' => $data['blessing_text_type_id'],
                'content'               => $data['content'],
                'update_time'           => Time::current(),
                'create_time'           => Time::current(),
            ]);
        return $this->json('', 200, 'ok');
    }

    /**
     * 获取后台传的祝福语
     * @param $id
     * @return \App\Http\Utils\Response
     */
    public function getOneBlessingText($id)
    {
        $dt = DB::table('blessing_text as bt')
            ->join(
                'blessing_text_type as btt',
                'id', '=', 'bt.blessing_text_type_id',
                'left'
            )
            ->select([
                'bt.id',
                'bt.blessing_text_type_id',
                'bt.content',
                'bt.update_time',
                'btt.name as blessing_text_type_name',
            ])
            ->where([
                ['bt.id', '=', $id]
            ])->first();
        return $this->json($dt, 200, 'ok');
    }

    /**
     * 获取所有
     * @return \App\Http\Utils\Response
     */
    public function getBlessingTextList()
    {
        $dt = DB::table('blessing_text_type')
            ->get()->toArray();
        if (is_null($dt)){
            $dt = [];
        }
        foreach ($dt as $k=>$v){
            $dt[$k]->blessing_text = DB::table('blessing_text')
                ->where('blessing_text_type_id','=',$v->id)
                ->get()->toArray();
        }
        return $this->json($dt, 200, 'ok');
    }

    public function updateBlessingText(Request $request, $id)
    {
        $data = $request->all();
        $rules = [
            'blessing_text_type_id' => 'required',
            'content'               => 'required',
        ];

        $messsages = [
            'blessing_text_type_id.required' => '类型不能为空',
            'content.required'               => '内容不能为空'
        ];
        $validator = Validator::make($data, $rules, $messsages);
        if ($validator->fails()) {
            return $this->json('', 417, $validator->getMessageBag()->getMessages());
        }
        DB::table('blessing_text')
            ->where([
                ['id', '=', $id]
            ])
            ->update([
                'blessing_text_type_id' => $data['type'],
                'content'               => $data['content'],
                'update_time'           => Time::current(),
            ]);
        return $this->json('', 200, 'ok');
    }

    /**
     * 删除祝福语类型
     * @param $id
     * @return \App\Http\Utils\Response
     */
    public function delBlessingTextType($id)
    {
        $isc = DB::table('blessing_text')
            ->where('blessing_text_type_id', '=',$id)
            ->first();
        if ($isc) {
            return $this->json('', 417, '该类型下还有祝福语');
        }

        if (DB::table('blessing_text_type')->delete($id)) {
            return $this->json('', 200, '删除成功');
        } else {
            return $this->json('', 417, '删除失败');
        }
    }

    /**
     * 删除祝福语
     * @param $id
     * @return \App\Http\Utils\Response
     */
    public function delBlessingText($id)
    {
        if (DB::table('blessing_text')->delete($id)) {
            return $this->json('', 200, '删除成功');
        } else {
            return $this->json('', 417, '删除失败');
        }
    }
}
